import dva from 'dva';
import {userSearch} from '../services/user';

export default {
  namespace: 'users',
  state: {
    list: [],
    loading: true
  },
  reducers: {
    search(state, {payload: {data: data}}) {
      return {...state, ...data.data, ...{loading:false}};
    },
  },
  effects: {
    *fetch({payload: {page}}, {call, put}) {
      const {data} = yield call(userSearch, {page});
      yield put({type: 'search', payload: {data: data}});
    }
  },
  subscriptions: {
    setup({dispatch, history}) {
      return history.listen(({pathname, query}) => {
        if (pathname === '/users') {
          dispatch({type: 'fetch', payload: query});
        }
      });
    }
  }
};
